'use strict';

const Controller = require('egg').Controller;

class EsController extends Controller {
  async find() {
    const { ctx, app } = this;
    const res = await app.elasticsearch.search({
      index: 'estest',
      size: 1,
    });
    ctx.body = res;
  }
  async add() {
    const { ctx, app } = this;
    const { title, content } = ctx.request.body;
    const res = await app.elasticsearch.index({ // 不提供id自动生产并创建
      index: 'estest',
      body: {
        title,
        content,
      },
    });
    ctx.body = res;
  }
  // async add() {
  //   const { ctx, app } = this;
  //   const { title, content } = ctx.request.body
  //   const res = await app.elasticsearch.create({   //必须提供id
  //     index: "estest",
  //     type: "_doc",
  //     id:"456",
  //     body: {
  //       title,
  //       content
  //     }
  //   });
  //   ctx.body = res
  // }
  async set() {
    const { ctx, app } = this;
    const { id, title, content } = ctx.request.body;
    const res = await app.elasticsearch.update({ //
      index: 'estest',
      id,
      body: {
        title,
        content,
      },
    });
    ctx.body = res;
  }
  async del() {
    const { ctx, app } = this;
    const { id } = ctx.request.body;
    const res = await app.elasticsearch.delete({
      index: 'estest',
      type: '_doc',
      id,
    });
    console.log('res', res);
    ctx.body = res;
  }
  async aggsFind() {
    const { ctx, app } = this;
    const res = await app.elasticsearch.search({
      index: 'estest',
      body: {

      },
    });
    ctx.body = res;
  }
}

module.exports = EsController;
